package com.example.lanouhn.firstapp.activity.utils;


import com.alibaba.fastjson.JSON;
import com.example.lanouhn.firstapp.activity.model.HomePage;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * JSON解析首页界面的item数据
 * Created by lanouhn on 16/7/19.
 */
public class HomePageJson {

    /**
     * 解析首页商品列表
     *
     * @param json
     * @return
     */
    public static List<HomePage> getHomePage(String json) {
        //创建数组来储存数据
        List<HomePage> list = new ArrayList<>();

        try {
            JSONObject job1 = new JSONObject(json);
            //获取解析的数据
            JSONArray array = job1.getJSONArray("datas");
            for (int i = 0; i <array.length() ; i++) {
                JSONObject jo = array.getJSONObject(i+1);
                String goods = jo.getString("goods");
                JSONObject job3 = new JSONObject(goods);
                //解析数据
                JSONArray array1 = job3.getJSONArray("item");

                //使用fastJson解析
                list = new ArrayList<>(JSON.parseArray(array1.toString(), HomePage.class));

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;

    }
}
